﻿using System;
using System.Data;
using System.Web.Security;
using System.Web.UI;
using MySql.Data.MySqlClient;

public partial class AddCustomer : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    MySqlCommand cmd = new MySqlCommand();

    static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["LoginName"] == null || Session["UserId"] == null)
        {
            Response.Redirect("Home.aspx");
        }
        else
        {
            if (Session["UserTypeId"] != null && Convert.ToInt32(Session["UserTypeId"]) == 1)
            {
            }
            else
            {
                FormsAuthentication.SignOut();
                Session["LoginName"] = "";
                Session["UserType"] = "";
                Session["MobileNumber"] = "";
                Session["EmailId"] = "";
                Response.Redirect("Home.aspx");
            }
        }
    }

    protected void btnCreateCustomer_Click(object sender, EventArgs e)
    {
        try
        {
            Customer c = new Customer();
            c.name = tbCustomerName.Text;
            c.address = tbAddress.Text;
            c.email = tbEmail.Text;
            c.contact_person = tbContactPerson.Text;
            c.contact_number = tbContactNumber.Text;

            bool customerExists = EventDAO.checkCustomerExists(c.name);

            if (customerExists == false)
            {
                int customerId = EventDAO.addCustomer(c);
                if (customerId != 0)
                {
                    Response.Redirect("Customers.aspx");
                }
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key_name", "alert('The customer is already in our system. Please enter a different name.');", true);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
}
